<?php
    $specialKey = 'system';
    $specialKeys = [
        'system',
        'gitpull'
    ];
    $topDirPath = dirname(__FILE__) . '/';
    require_once($topDirPath . $specialKey . '/libs/php/fns.php');

    $iniFile = 'info.json'; // 默认 {"name":"","desc":"","tail":"","origin":""}
    $iniArr = [
        'dir' => '',
        'name' => '',
        'desc' => '',
        'tail' => '',
        'url' => '',
        'origin' => '',
        'versions' => [
            // [
            //     'name' => '',
            //     'url' => ''
            // ]
        ]
    ];


    function cleanListByArray ($arr) {
        $return = [];
        global $specialKeys;
        foreach ($arr as $one) {
            if (!in_array($one, $specialKeys)) {
                $return[] = $one;
            }
        }
        return $return;
    }
    function cleanList ($path) {
        $arr = get_list_of_folders_by_path_not_recursive($path);
        return cleanListByArray($arr);
    }
    

    // 接口
    if (isset($_GET['api']) && $_GET['api']) {
        header('Content-type: text/json; charset=utf-8');
        switch ($_GET['api']) {
            case 'list':
                $listTop = cleanList($topDirPath);
                $list = [];

                // 添加直达站点列表 - 直接线上读取
                require_once('./index-online-sites.php');
                $list = $listOnlineSites;

                foreach ($listTop as $oneTop) {
                    $oneTopMixed = $iniArr;
                    $oneTopMixed['dir'] = $oneTop;
                    $oneTopMixed['name'] = $oneTop;
                    $oneTopMixed['desc'] = $oneTop;
                    $oneTopMixed['url'] = '/' . $oneTop . '/';
                    $path = $topDirPath . $oneTop . '/';
                    $ini = $path . $iniFile;
                    if (file_exists($ini)) {
                        $c = json_decode(file_get_contents($ini), true);
                        if ($c) {
                            $oneTopMixed['name'] = $c['name'];
                            $oneTopMixed['desc'] = $c['desc'];
                            $oneTopMixed['tail'] = $c['tail'];
                            $oneTopMixed['origin'] = $c['origin'];
                        }
                    }

                    // 获取不同日期(版本)
                    $subList = cleanList($path);
                    foreach ($subList as $oneSL) {
                        $oneTopMixed['versions'][] = [
                            'name' => $oneSL,
                            'url' => '/' . $oneTop . '/' . $oneSL .'/'. $oneTopMixed['tail']
                        ];
                    }
                    $list[] = $oneTopMixed;
                }

                // 单个
                if (isset($_POST['name']) && $_POST['name']) {
                    $listAll = $list;
                    $list = [];
                    foreach ($listAll as $one) {
                        if ($one['dir'] == $_POST['name']) {
                            $list[] = $one;
                        }
                    }
                }

                echo json_encode(['code'=>1,'msg'=>'','data'=>$list]);
                die();
                break;

            default:
                echo json_encode(['code'=>0,'msg'=>'未知api','data'=>[]]);
                die();
                break;
        }
        die();
    }
?>
<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="icon" href="/system/assets/logo/lightbulb.png"/>
        <title>手册加速-Rudon</title>

        <link href="/system/assets/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/>
        <link href="/system/assets/css/rudon.css" rel="stylesheet"/>
        
        <script src="/system/assets/jquery/3.6.0/jquery.min.js"></script>
        <script src="/system/assets/bootstrap/3.4.1/js/bootstrap.min.js"></script>

        <script src="/system/mirror/unpkg.com/vue@3.4.23/dist/vue.global.prod.js"></script>


        <style type="text/css">
            a {
                text-decoration: none!important;
            }
        
            .smallBox div {
                margin-top: 30px;
                /*padding-bottom: 30px;*/ 
                /*display: inline-block;*/
            } 

            .innerWrapper {
                border: 1px #dde solid; border-radius: 9px; min-height: 180px;  background: rgb(247,247,247);
                padding: 10px;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <div id="app">

            <nav class="navbar navbar-inverse">
                <div class="container">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                            <span class="sr-only">Togglenavigation
                            </span>
                            <span class="icon-bar">
                            </span>
                            <span class="icon-bar">
                            </span>
                            <span class="icon-bar">
                            </span>
                        </button>
                        <a class="navbar-brand" href="/" style="color: white;">
                            <img src="/system/assets/logo/lightbulb.png" alt="" style="height: 28px;filter: invert(100%); display: inline-block; margin-right: 8px; margin-top: -3px;" class=""/>
                            手册加速 <span class="opacity30">- Rudon</span>
                        </a>
                    </div>
                    <div>
                        <form action="" method="post" onsubmit="return false" class="navbar-form navbar-right">
                            <div class="form-group">
                                <input type="text" placeholder="Search" class="form-control" v-model="pd.search"/>
                            </div>
                        </form>
                    </div>
                    <!--/.navbar-collapse -->
                </div>
            </nav>
            
            <div class="container minHeight500px">
                <!-- Example row of columns -->
                <div class="row">
                    
                    <div 
                        class="col-md-4 smallBox"
                        v-for="(item, index) in filteredList"
                    >
                        <div class="innerWrapper" @click.stop="go(item.url)">
                            <a :href="item.url" @click.stop>
                                <h3>
                                    {{ item.name }}
                                </h3>
                            </a>
                            <p>
                                {{ item.desc }}
                            </p>
                            <div class="rowStartAlignCenter flexAutoLineBreaking">
                                <a 
                                    @click.stop
                                    target="_blank" 
                                    :href="itemS.url"
                                    v-for="(itemS, indexS) in item.versions"
                                    :key="'indexS'+indexS"
                                    class="label label-primary marginBottom5 marginRight10 fontSize14"
                                >
                                    {{ itemS.name }}
                                </a>
                            </div>
                        </div>
                    </div>
                    
                </div>
                <hr>
                <br />

                <div class="beian-wrapper" style="position: fixed; z-index: 100100; bottom: 10px; left: 0; text-align: center; width: 99%;" ondblclick="this.style.display = 'none';">
                    <a href="https://beian.miit.gov.cn" target="_blank" style="text-decoration: none; color: #9C9C9C; font-size:12px;" title="粤ICP备2024301841号-1">
                        粤ICP备2024301841号
                    </a>
                </div>
            </div>
            <!-- /container -->

            
            
            <div style="text-align: center; font-size: 18px;">
                <a href='/system/commit.html'>留言、讨论</a>
            </div>
            
            <br/>
            <br/>
            <br/>
            <br/>
            
        </div>

        <script>
            const { createApp, ref, reactive, onMounted, computed } = Vue
            
            createApp({
                setup() {
                    const pd = reactive({
                        search: '',
                        list: []
                    })

                    const clog = (...args) => {
                        console.log(...args)
                    }

                    const go = (url) => {
                        window.location.href = url
                    }

                    const filteredList = computed(()=>{
                        let res = []
                        let allList = pd.list
                        let kw = pd.search
                        if (kw === '') {
                            res = allList
                        } else {
                            for (let i of allList) {
                                if (i.name.includes(kw) || i.desc.includes(kw) || i.origin.includes(kw)) {
                                    res.push(i)
                                }
                            }
                        }
                        return res
                    })
                    

                    const initList = () => {
                        ajaxPost('/?api=list').then(r=>{
                            clog(r)
                            if (!r) {
                                return alert('获取列表失败')
                            }
                            if (r.code != 1) {
                                return alert(r.msg)
                            }
                            pd.list = r.data
                            clog(pd.list)
                        }).catch(e=>{
                            clog(e)
                            alert('获取列表失败')
                        }).finally(()=>{

                        })
                    }

                    const ajaxPost = (url, data = {}) => {
                        return new Promise((resolve, reject)=>{
                            $.ajax({
                                url: url,
                                method: 'post',
                                dataType: 'json',
                                data: data,
                                success: r=>{
                                    resolve(r)
                                },
                                error: e=>{
                                    reject(e)
                                }
                            })
                        })
                    }

                    onMounted(()=>{
                        initList()
                    })

                    return {
                        pd,
                        go,
                        filteredList
                    }
                }
            }).mount('#app')
            </script>
    </body>
</html>
